// 评分系统
export default function() {
	// 积分对应的星级
	const starList = [
		{ star: 1, limit: [0, 40] }, 
		{ star: 2, limit: [40, 65] }, 
		{ star: 3, limit: [65, 80] }, 
		{ star: 4, limit: [80, 95] }, 
		{ star: 5, limit: [95, 100] }
	]
	// 基准
	const base = {
		kpm: 60,
		accuracy: 10,
		back: 10
	}
	// 基准
	// 分配权重
	const weight = {
		kpm: 0.1,
		accuracy: 0.8,
		back: 0.1
	}
	/**
	 * 获取总分数
	 * @param {Object} accuracy 准确率
	 * @param {Object} maxKpm 最大打字速度
	 * @param {Object} kpm 打字速度
	 * @param {Object} back 退格次数
	 */
	function getScore({ maxKpm, kpm, back, accuracy } = {}) {
		console.log(maxKpm, kpm, back, accuracy)
		maxKpm = maxKpm ? maxKpm : 1;
		// 标准化速度
		let _kpm = (kpm - base.kpm > 0 ? kpm - base.kpm : 0) / (maxKpm - base.kpm);
		_kpm = Math.max(0, Math.min(1, _kpm));
		// 标准化正确率
		let _accuracy = ((accuracy - base.accuracy > 0 ? accuracy - base.accuracy : 0) / 100) / (1 - base.accuracy / 100);
		_accuracy = Math.max(0, Math.min(1, _accuracy));
		// 标准化退格数
		let _back = 1 - (back / base.back); //0次退格得1分，base.back次及以上得0分
		_back = Math.max(0, Math.min(1, _back));
		if(accuracy < 10) {
			weight.kpm = 0;
			weight.back = 0;
		}
		// 计算加权评分
		let total = _kpm * weight.kpm + _accuracy * weight.accuracy + _back * weight.back;
		return Math.round(total * 100);
	}
	/**
	 * 获取评分
	 */
	function getStars(options = {}) {
		const _score = getScore(options);
		let _star = 1;
		starList.forEach(item => {
			if (_score >= item.limit[0] && _score <= item.limit[1]) {
				_star = item.star;
			}
		})
		_star = _score == 0 ? 0 : _star;
		return {
			score: _score,
			star: _star
		}
	}
	return {
		getStars
	}
}